﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace delegeteandevent
{
    //定义事件参数
    public  class UserEventArgs:EventArgs
    {
        public string email;
        public string Mobile;
        public string Amount;

        public UserEventArgs(string email, string mobile, string amount)
        {
            this.email = email;
            Mobile = mobile;
            Amount = amount;
        }
    }

    //事件发布者 银行

    public class Bank
    {
        //声明委托：银行将以
        public delegate void ProcessTranEventHandler(Object sender,UserEventArgs e);
        //声明时间
        public event ProcessTranEventHandler ProcessTran;

        protected virtual void OnProcessTran(UserEventArgs e)
        {
            if (ProcessTran != null)
            {
                ProcessTran(this, e);
            }
        }

        //触发事件
        public void  Process(UserEventArgs  e)
        {
            OnProcessTran(e);
        }
    }

    //事件观察者：电子邮件
    public class Email
    {
        public  static  void  SendMail(Object sender,UserEventArgs e)
        {
            Console.WriteLine($"向用户邮箱{e.email}发送邮件：您在{System.DateTime.Now}取款金额为{e.Amount }元");
        }
    }

    //时间观察者：短信
    public class MobileSMS
    {
        public static void SendNotification(Object sender, UserEventArgs e)
        {
            Console.WriteLine($"向用户邮箱{e.Mobile }发送短信：您在{System.DateTime.Now}取款金额为{e.Amount }元");
        }
    }
    //订阅系统：松耦合
    public  class  SubscribeSystem
    {
        public SubscribeSystem() { }
        public SubscribeSystem(Bank bank, UserEventArgs e)
        {
            bank.ProcessTran += Email.SendMail;
            bank.ProcessTran += MobileSMS.SendNotification;
            bank.Process(e);
        }
        
    }
}
